﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using DevComponents.DotNetBar;
using PlanningGIS.Win.Controls.Properties;

namespace PlanningGIS.Win.Controls
{
    /// <summary>
    /// 口令设置界面
    /// </summary>
    public partial class CtlPassword : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CtlPassword()
        {
            InitializeComponent();
            MinLength = 6;
            AllowFZD = true;
        }

        /// <summary>
        ///确认输入的密码
        /// </summary>
        private string PasswordOk = null;

        /// <summary>
        /// 用户新密码
        /// </summary>
        public string Password
        {
            get { return PasswordOk; }
            set
            {
                txtNewpassword.Text = value;
                txtConfirmNewpassword.Text = value;
            }
        }

        /// <summary>
        /// 是否启用密码复杂度
        /// </summary>
        public bool AllowFZD { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public bool ReadOnly
        {
            get { return txtNewpassword.ReadOnly; }
            set
            {
                txtNewpassword.ReadOnly = value;
                txtConfirmNewpassword.ReadOnly = value;
            }
        }
        
        /// <summary>
        /// 密码最小长度
        /// </summary>
        public int MinLength { get; set; }

        /// <summary>
        ///密码强弱色度带
        /// </summary>
        string passworColordTip = null;
        /// <summary>
        ///密码强弱级别
        /// </summary>
        private int SecurityTag = -1;

        /// <summary>
        /// 密码确认文本框控制
        /// </summary>
        bool txtInputChanged = false;

        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PasswordControl_Load(object sender, EventArgs e)
        {           
            lblSecurityTip.Visible = false;
            lblWeak.Visible = false;
            lblMiddle.Visible = false;
            lblStrong.Visible = false;
            picBoxInfo1.Visible = false;
            picBoxInfo2.Visible = false;
        }

        /// <summary>
        /// 密码最大长度
        /// </summary>
        public int MaxLength
        {
            get { return txtNewpassword.MaxLength; }
            set
            {
                txtNewpassword.MaxLength = value;
                txtConfirmNewpassword.MaxLength = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public char PasswordChar
        {
            get { return txtNewpassword.PasswordChar; }
            set
            {
                txtNewpassword.PasswordChar = value;
                txtConfirmNewpassword.PasswordChar = value;
            }
        }

        /// <summary>
        /// 新密码输入框鼠标按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNewpassword_MouseDown(object sender, MouseEventArgs e)
        {
            if (txtNewpassword.TextLength == 0 && AllowFZD)
            {
                string Info = "请输入" + MinLength.ToString() + "～" + MaxLength.ToString() + "位半角字符（必须是大小写字母、数字、特殊符号两种或两种以上组合）";
                if (MaxLength==0)
                    Info = "请输入" + MinLength.ToString() + "位及以上的半角字符（必须是大小写字母、数字、特殊符号两种或两种以上组合）";
                //InfoTip.DefaultBalloonWidth = 150;
                InfoTip.SetBalloonText(txtNewpassword, Info);
                InfoTip.ShowBalloon(txtNewpassword);
            }
        }
        /// <summary>
        /// 新密码输入框TextChanged事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNewpassword_TextChanged(object sender, EventArgs e)
        {
            InfoTip.Remove(txtNewpassword);
            lblWeak.Visible = false;
            lblMiddle.Visible = false;
            lblStrong.Visible = false;
            lblSecurityTip.Visible = false;
            lbltxtConfirmtip.Visible = false;
            picBoxInfo2.Visible = false;
            lblSecurity.Visible = false;
            picBoxInfo1.Visible = false;
            PasswordOk = null;
            if (!AllowFZD) return;
            string SecurityIndex = PasswordVerify(txtNewpassword.Text.Trim()).ToString();
            switch (SecurityIndex)
            {
                case "-1":
                    lblSecurityTip.Visible = false;
                    break;
                case "0":
                    lblSecurityTip.Visible = false;
                    break;
                case "1":
                    lblSecurityTip.Width = 40;
                    lblSecurityTip.BackColor = Color.FromArgb(255, 192, 192);
                    lblWeak.BackColor = Color.FromArgb(255, 192, 192);
                    lblWeak.Visible = true;
                    lblSecurityTip.Visible = true;
                    passworColordTip = lblWeak.Text;
                    txtConfirmNewpassword.BackColor = txtNewpassword.BackColor;
                    break;
                case "2":
                    lblSecurityTip.Width = 80;
                    lblSecurityTip.BackColor = Color.FromArgb(255, 255, 128);
                    lblMiddle.BackColor = Color.FromArgb(255, 255, 128);
                    lblMiddle.Visible = true;
                    lblSecurityTip.Visible = true;
                    passworColordTip = lblMiddle.Text;
                    txtConfirmNewpassword.BackColor = txtNewpassword.BackColor;
                    break;
                case "3":
                    lblSecurityTip.Width = 100;
                    lblSecurityTip.BackColor = Color.FromArgb(80, 220, 128);
                    lblStrong.BackColor = Color.FromArgb(80, 220, 128);
                    lblStrong.Visible = true;
                    lblSecurityTip.Visible = true;
                    passworColordTip = lblStrong.Text;
                    txtConfirmNewpassword.BackColor = txtNewpassword.BackColor;
                    break;
            }
        }
        ///<summary>
        /// MouseLeave事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNewpassword_MouseLeave(object sender, EventArgs e)
        {
            InfoTip.Remove(txtNewpassword);
            txtNewpasswordMouse(true);
        }
        /// <summary>
        /// 键盘KeyDown事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNewpassword_KeyDown(object sender, KeyEventArgs e)
        {
            InfoTip.Remove(txtNewpassword);
            if (e.KeyCode == Keys.Enter)
            {
                txtNewpasswordMouse(true);
            }
        }
        /// <summary>
        /// Leave事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNewpassword_Leave(object sender, EventArgs e)
        {
            InfoTip.Remove(txtNewpassword);
            txtNewpasswordMouse(true);
        }
        /// <summary>
        ///txtNewpassword鼠标和Enter键方法
        /// </summary>
        /// <param name="occur">鼠标或者Enter键按下</param>
        private void txtNewpasswordMouse(bool occur)
        {
            if (!AllowFZD)
            {
                txtInputChanged = true;
                return;
            }
            if (occur)
            {
                if (SecurityTag == -1)
                {
                    if (txtNewpassword.TextLength == 0)
                    {
                        picBoxInfo1.Visible = false;
                    }
                    else
                    {
                        picBoxInfo1.Image = Resources.failure;
                        picBoxInfo1.Visible = true;
                        lblSecurity.Text = "密码长度不够！";
                        lblSecurity.ForeColor = Color.Red;
                        lblSecurity.Visible = true;
                    }
                    txtInputChanged = false;
                }
                else if (SecurityTag == 0)
                {
                    lblSecurity.Text = "密码复杂度不够！";
                    lblSecurity.ForeColor = Color.Red;
                    lblSecurity.Visible = true;
                    picBoxInfo1.Image = Resources.failure;
                    picBoxInfo1.Visible = true;
                    txtInputChanged = false;
                }
                else
                {
                    lblWeak.Visible = false;
                    lblMiddle.Visible = false;
                    lblStrong.Visible = false;
                    lblSecurityTip.Visible = false;
                    picBoxInfo1.Visible = true;
                    picBoxInfo1.Image = Resources.success;
                    lblSecurity.Visible = true;
                    lblSecurity.ForeColor = Color.Green;
                    lblSecurity.Text = "密码强度:" + passworColordTip;
                    txtInputChanged = true;
                    if (txtConfirmNewpassword.TextLength == 0)
                    {
                        lbltxtConfirmtip.Text = "请再次输入密码！";
                        lbltxtConfirmtip.ForeColor = Color.Red;
                        lbltxtConfirmtip.Visible = true;
                    }
                    else
                    {
                        txtConfirmNewpasswordMouse(true);
                    }
                }
            }
        }
        /// <summary>
        /// MouseEnter事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNewpassword_MouseEnter(object sender, EventArgs e)
        {
            InfoTip.Remove(txtNewpassword);
        }
        /// <summary>
        /// Keypress事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNewpassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            InfoTip.Remove(txtNewpassword);
        }
        /// <summary>
        ///  密码级别验证
        /// </summary>
        /// <param name="InputPassword">新密码</param>
        private int PasswordVerify(string InputPassword)
        {
            string length = MinLength.ToString();
            string Number = "(?=.*[\\d].*)";
            string lowerLetter = "(?=.*[a-z].*)";
            string capitalLetter = "(?=.*[A-Z].*)";
            //string specialChars = "(?=.*[|'|‘|\"|“|/|、|:|：|;|；|!|！|@|◎|#|＃|(\\])|】|【|(\\[)||(\\})|}|(\\{)|{|。|(\\.)|(\\,)|，|(\\>)|》|(\\<)|《|(\\$)|￥|(\\|)|||￥|%|％|(\\^)|^|-|(\\&)|&|※|(\\*)|*|×|(\\()|（|(\\))|）|_|——|(\\+)|＋|(\\|)|§|?|)|].*)";
            string specialChars = "(?=.*[|" + "\\\\" + "|=|'|‘|\"|“|/|、|:|：|;|；|!|！|@|◎|#|＃|(\\])|】|【|(\\[)||(\\})|}|(\\{)|{|。|(\\.)|(\\,)|，|(\\>)|》|(\\<)|《|(\\$)|￥|(\\|)|%|％|(\\^)|^|-|(\\&)|&|※|(\\*)|*|×|(\\()|（|(\\))|）|_|——|(\\+)|＋|(\\|)|§|?|)|\\|、|].*)";
            //通用安全度密码
            Boolean Num = new Regex("^(?=.*[\\s\\S].*).{" + length + ",}$").IsMatch(InputPassword, 0);
            //单类型密码
            Boolean numberOnly = new Regex("^" + Number + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            Boolean capitalLetterOnly = new Regex("^" + capitalLetter + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            Boolean lowerLetterOnly = new Regex("^" + lowerLetter + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            Boolean specialCharsOnly = new Regex("^" + specialChars + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            //弱安全度密码
            Boolean Num_lowerLetter = new Regex("^" + Number + lowerLetter + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            Boolean Num_capitalLetter = new Regex("^" + Number + capitalLetter + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            Boolean Num_specialChars = new Regex("^" + Number + specialChars + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            Boolean lowerLetter_capitalLetter = new Regex("^" + lowerLetter + capitalLetter + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            Boolean lowerLetter_specialChars = new Regex("^" + lowerLetter + specialChars + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            Boolean specialChars_capitalLetter = new Regex("^" + specialChars + capitalLetter + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            //中安全度密码
            Boolean Num_cl = new Regex("^" + Number + capitalLetter + lowerLetter + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            Boolean Num_sl = new Regex("^" + Number + lowerLetter + specialChars + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            Boolean Num_cs = new Regex("^" + Number + capitalLetter + specialChars + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            Boolean cls = new Regex("^" + capitalLetter + lowerLetter + specialChars + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            //强安全度密码
            Boolean BothType = new Regex("^" + Number + capitalLetter + lowerLetter + specialChars + ".{" + length + ",}$").IsMatch(InputPassword, 0);
            //密码强弱动态判断
            if (txtNewpassword.TextLength < MinLength)
            {
                SecurityTag = -1;
            }
            else
            {
                if (Num == true)
                {
                    if (numberOnly == true || capitalLetterOnly == true || lowerLetterOnly == true || specialCharsOnly == true)
                    {
                        if (Num_lowerLetter == true || Num_capitalLetter == true || Num_specialChars == true || lowerLetter_capitalLetter == true
                         || lowerLetter_specialChars == true || specialChars_capitalLetter == true)
                        {
                            if (Num_cl == true || Num_sl == true || Num_cs == true || cls == true)
                            {
                                if (BothType == true)
                                {
                                    SecurityTag = 3;
                                }
                                else
                                {
                                    SecurityTag = 2;
                                }
                            }
                            else
                            {
                                SecurityTag = 1;
                            }
                        }
                        else
                        {
                            SecurityTag = 0;
                        }
                    }
                    else
                    {
                        SecurityTag = 0;
                    }
                }
            }
            return SecurityTag;
        }
        /// <summary>
        /// TextChanged事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtConfirmNewpassword_TextChanged(object sender, EventArgs e)
        {
            lbltxtConfirmtip.Visible = false;
            picBoxInfo2.Visible = false;
            txtConfirmNewpassword.BackColor = Color.FromName("Window"); ;
            if (txtInputChanged == true)
            {
                if (txtConfirmNewpassword.TextLength == 0)
                {
                    picBoxInfo2.Visible = false;
                    lbltxtConfirmtip.Visible = false;
                }
                else
                {
                    if (txtConfirmNewpassword.Text == txtNewpassword.Text)
                    {
                        picBoxInfo2.Visible = true;
                        picBoxInfo2.Image = Resources.success;
                        lbltxtConfirmtip.Visible = true;
                        lbltxtConfirmtip.Text = "";
                    }
                }
            }
        }
        /// <summary>
        /// MouseLeave事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtConfirmNewpassword_MouseLeave(object sender, EventArgs e)
        {
            PasswordOk = null;
            txtConfirmNewpasswordMouse(true);
        }
        /// <summary>
        /// KeyDown事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtConfirmNewpassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                PasswordOk = null;
                txtConfirmNewpasswordMouse(true);
            }
        }
        /// <summary>
        /// Leave事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtConfirmNewpassword_Leave(object sender, EventArgs e)
        {
            PasswordOk = null;
            txtConfirmNewpasswordMouse(true);
        }
        /// <summary>
        /// MouseDown事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtConfirmNewpassword_MouseDown(object sender, MouseEventArgs e)
        {
            txtConfirmNewpassword.BackColor = Color.FromName("Window"); ;
        }
        /// <summary>
        ///确认密码时鼠标和Enter键事件调用
        /// </summary>
        /// <param name="occur">鼠标或者Enter键按下</param>
        private void txtConfirmNewpasswordMouse(Boolean occur)
        {
            if (occur == true)
            {
                if (txtInputChanged == true)
                {
                    if (txtConfirmNewpassword.TextLength == 0)
                    {
                        lbltxtConfirmtip.Text = "请再次输入密码！";
                        lbltxtConfirmtip.ForeColor = Color.Red;
                        lbltxtConfirmtip.Visible = true; ;
                    }
                    else
                    {
                        if (txtConfirmNewpassword.Text.Trim() == txtNewpassword.Text.Trim())
                        {
                            if (txtConfirmNewpassword.Text.Trim().Length >= MinLength || !AllowFZD)
                            {
                                picBoxInfo2.Visible = true;
                                picBoxInfo2.Image = Resources.success;
                                lbltxtConfirmtip.Visible = true;
                                //lbltxtConfirmtip.ForeColor = Color.Green;
                                //bltxtConfirmtip.Text = "密码输入正确！";
                                lbltxtConfirmtip.Text = "";
                                // txtConfirmNewpassword.BackColor = Color.FromArgb(250, 226, 226);
                                PasswordOk = txtConfirmNewpassword.Text.Trim();
                            }
                            else
                            {
                                picBoxInfo2.Visible = false;
                                lbltxtConfirmtip.Visible = false;
                            }
                        }
                        else
                        {
                            picBoxInfo2.Visible = true;
                            picBoxInfo2.Image = Resources.failure;
                            lbltxtConfirmtip.Visible = true;
                            lbltxtConfirmtip.ForeColor = Color.Red;
                            lbltxtConfirmtip.Text = "两次输入密码不同！";
                        }
                    }
                }
                else
                {
                    if (txtConfirmNewpassword.Text.Trim() != txtNewpassword.Text.Trim())
                    {
                        if (txtConfirmNewpassword.TextLength != 0 && txtNewpassword.TextLength == 0)
                        {
                            picBoxInfo2.Visible = true;
                            picBoxInfo2.Image = Resources.failure;
                            lbltxtConfirmtip.Visible = true;
                            lbltxtConfirmtip.ForeColor = Color.Red;
                            lbltxtConfirmtip.Text = "两次输入密码不同！";
                        }
                        else
                        {
                            lbltxtConfirmtip.Visible = false;
                            picBoxInfo2.Visible = false;
                            lbltxtConfirmtip.Visible = false;
                        }
                    }
                    else
                    {
                        lbltxtConfirmtip.Visible = false;
                        picBoxInfo2.Visible = false;
                        lbltxtConfirmtip.Visible = false;
                    }
                }
            }
        }
    }
}

